package cn.wizzer.test;



import cn.wizzer.util.CompareUtil;
import org.joda.time.DateTime;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@SuppressWarnings("all")
public class CheckOnTest02 {


    // 获取天凌晨的时间
    public static final DateTime CUZERO =getCurrentZero();
    // 获取第二天凌晨的时间



    public static final DateTime NEZERO =getCurrentZero();

    public static final DateTime CURRENT = getCurrent();

    public static final DateTime AMON =getAMON();// 上午上班打卡时间
    public static final DateTime AMOFF = getAMOFF(); // 上午下班打卡时间
    public static final DateTime PMON =getPMON(); // 下午上班时间
    public static final DateTime PMOFF = getPMOFF(); // 下午下班时间


    /**
     * 打卡机打卡记录传递到后台有 in out 属性,
     * 并且每个人在一天内打卡的次数是不固定的,
     * 需要在区间内取最后一次打卡记录
     * 打卡场景:
     * 1. 上班打卡后又外出,随后又打卡进入
     *  需要判断打卡人外出后在下班时间段内有无返回工地,如果没有返回工地就以早退处理
     *
     *  假设考勤机传递给我的是一个列表,列表中按顺序排列了某工友一天所有的打卡记录
     *  1. 获取所有进场的打卡记录
     *   1.1. 在上午下工之前最早的一次打卡肯定是上班打卡,
     *   1.2. 在上午下工之后,并且在下午下工之前最早的一次打卡肯定是下午上班打卡
     *  2. 获取所有离场的打卡记录
     *   2.1 在下午上工之前的最后一次打卡肯定是上午下班打卡
     *   2.2 在下午上工之前的最后一次打卡之外的最后一次打卡肯定是下午下班打卡
     *
     */

    @Test
    public void  getRecord(){
        List<DateTime> inRecord = new ArrayList<>();
        List<DateTime> outRecord = new ArrayList<>();

        DateTime amonless = getCurrent(7, 30, 00);
        DateTime amon = getCurrent(8, 30, 00);
        DateTime amonmore = getCurrent(9, 30, 00);
        DateTime amoffless = getCurrent(11, 00, 00);
        DateTime amoff = getCurrent(11, 30, 00);
        DateTime amoffmore = getCurrent(11, 40, 00);
        DateTime pmonless = getCurrent(12, 00, 00);
        DateTime pmon = getCurrent(12, 30, 00);
        DateTime pmonmore = getCurrent(13, 00, 00);
        DateTime pmoffless= getCurrent(18, 00, 00);
        DateTime pmoff = getCurrent(18, 30, 00);
        DateTime pmoffmore = getCurrent(19, 00, 00);
        inRecord.addAll(Arrays.asList(amonless,amon,amonmore,pmonless,pmon,pmonmore));
        outRecord.addAll(Arrays.asList(amoffless,amoff,amoffmore,pmoffless,pmoff,pmoffmore));

        DateTime real_amon  = null;
        DateTime real_amoff  = null;
        DateTime real_pmon  = null;
        DateTime real_pmoff  = null;



        /**
         * 处理进场打卡记录
         *   打卡时间在上午下工之前,并且是最小的
         *   1. 过滤上午下工之前所有的进场记录
         *   2. 获取最小的进场时间
         *
         */

        List<DateTime> collect = inRecord.stream().filter(in -> in.isBefore(AMOFF.getMillis())).collect(Collectors.toList());
        if (collect!=null&&collect.size()>0)
        real_amon = collect.stream().min(CompareUtil::comparesByDateDesc).get();

        collect= inRecord.stream().filter(in->(in.isAfter(AMOFF.getMillis()))&&in.isBefore(PMOFF.getMillis())).collect(Collectors.toList());
        if (collect!=null&&collect.size()>0)
        real_pmon = collect.stream().min(CompareUtil::comparesByDateDesc).get();

        /**
         * 处理离场打卡记录
         *  1.在下午上工之前的最后一次打卡肯定是上午下班打卡
         *  2.在下午上工之后的最后一次打卡肯定是下午下班打卡
         *
         */
        collect = outRecord.stream().filter(out->out.isBefore(PMON.getMillis())).collect(Collectors.toList());
        if (collect!=null&&collect.size()>0)
        real_amoff = collect.stream().max(CompareUtil::comparesByDateDesc).get();
       // System.out.println(real_amoff);

       collect =  outRecord.stream().filter(out->out.isAfter(PMON.getMillis())).collect(Collectors.toList());
        if (collect!=null&&collect.size()>0)
       real_pmoff = collect.stream().max(CompareUtil::comparesByDateDesc).get();


        System.out.println("上午上班打卡时间:"+real_amon);
        System.out.println("上午下班打卡时间:"+real_amoff);
        System.out.println("下午上班打卡时间:"+real_pmon);
        System.out.println("下午下班打卡时间:"+real_pmoff);


    }










    public static DateTime getAMON(){
        Date date = new Date();
        DateTime dateTime = new DateTime(date);
        int year = dateTime.getYear();
        int monthOfYear = dateTime.getMonthOfYear();
        int dayOfMonth = dateTime.getDayOfMonth();

        DateTime dateTime2 = new DateTime(year,monthOfYear,dayOfMonth,9,0,0);
        return dateTime2;
    }

    public static DateTime getAMOFF() {
        Date date = new Date();
        DateTime dateTime = new DateTime(date);
        int year = dateTime.getYear();
        int monthOfYear = dateTime.getMonthOfYear();
        int dayOfMonth = dateTime.getDayOfMonth();

        DateTime dateTime2 = new DateTime(year,monthOfYear,dayOfMonth,11,0,0);
        return dateTime2;

        //return AMOFF;
    }

    public static DateTime getPMON(){
        Date date = new Date();
        DateTime dateTime = new DateTime(date);
        int year = dateTime.getYear();
        int monthOfYear = dateTime.getMonthOfYear();
        int dayOfMonth = dateTime.getDayOfMonth();

        DateTime dateTime2 = new DateTime(year,monthOfYear,dayOfMonth,13,0,0);
        return dateTime2;
    }

    public static DateTime getPMOFF() {
        Date date = new Date();
        DateTime dateTime = new DateTime(date);
        int year = dateTime.getYear();
        int monthOfYear = dateTime.getMonthOfYear();
        int dayOfMonth = dateTime.getDayOfMonth();

        DateTime dateTime2 = new DateTime(year,monthOfYear,dayOfMonth,17,0,0);
        return dateTime2;

        //return AMOFF;
    }
    public static DateTime getCurrentZero() {
        Date date = new Date();
        DateTime dateTime = new DateTime(date);
        int year = dateTime.getYear();
        int monthOfYear = dateTime.getMonthOfYear();
        int dayOfMonth = dateTime.getDayOfMonth();

        DateTime dateTime2 = new DateTime(year,monthOfYear,dayOfMonth,0,0,0);
        return dateTime2;


    }
    public static DateTime getNextZero() {
        Date date = new Date();
        DateTime dateTime = new DateTime(date);
        int year = dateTime.getYear();
        int monthOfYear = dateTime.getMonthOfYear();
        int dayOfMonth = dateTime.getDayOfMonth();

        DateTime dateTime2 = new DateTime(year,monthOfYear,dayOfMonth,0,0,0).plusDays(1);
        return dateTime2;


    }

    public static DateTime getCurrent() {
        Date date = new Date();
        DateTime dateTime = new DateTime(date);
        int year = dateTime.getYear();
        int monthOfYear = dateTime.getMonthOfYear();
        int dayOfMonth = dateTime.getDayOfMonth();

        DateTime dateTime2 = new DateTime(year,monthOfYear,dayOfMonth,7,0,0);
        return dateTime2;
    }
    public static DateTime getCurrent(int hh,int mm ,int ss) {
        Date date = new Date();
        DateTime dateTime = new DateTime(date);
        int year = dateTime.getYear();
        int monthOfYear = dateTime.getMonthOfYear();
        int dayOfMonth = dateTime.getDayOfMonth();

        DateTime dateTime2 = new DateTime(year,monthOfYear,dayOfMonth,hh,mm,ss);
        return dateTime2;
    }

}
